#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#define N 5

void ChildFun()
{
    int cnt = 5;
    while(cnt)
    {
        printf("I am child PID:%d,PPID:%d,cnt:%d\n",getpid(),getppid(),cnt);
        cnt--;
        sleep(1);
    }

}

int main()
{
    int i = 0;
    for(i = 0;i<N;i++)
    {
        pid_t id = fork();
        if(id<0)
        {
            perror("fork");
            exit(0);
        }
        else if(id==0)
        {
            //child
            ChildFun();
            exit(1);
        }
        printf("create child process: %d succees\n",id);
    }


    //parent
    int cnt = 10;
    while(cnt)
    {

        printf("I am father PID:%d,PPID:%d,cnt:%d\n",getpid(),getppid(),cnt);
        cnt--;
        sleep(1);
    }
    for(i = 0;i<N;i++)
    {
        pid_t id = wait(NULL);
        if(id>0)
        {
            printf("wait %d success\n",id);
        }

    }
    sleep(5);
    return 0;
}
